Skip to main content
ICT
Lesson A10 - The String Class
 
Main Previous Next
Title Page >  
Summary >  
Lesson A1 >  
Lesson A2 >  
Lesson A3 >  
Lesson A4 >  
Lesson A5 >  
Lesson A6 >  
Lesson A7 >  
Lesson A8 >  
Lesson A9 >  
Lesson A10 >  
Lesson A11 >  
Lesson A12 >  
Lesson A13 >  
Lesson A14 >  
Lesson A15 >  
Lesson A16 >  
Lesson A17 >  
Lesson A18 >  
Lesson A19 >  
Lesson A20 >  
Lesson A21 >  
Lesson A22 >  
Lesson AB23 >  
Lesson AB24 >  
Lesson AB25 >  
Lesson AB26 >  
Lesson AB27 >  
Lesson AB28 >  
Lesson AB29 >  
Lesson AB30 >  
Lesson AB31 >  
Lesson AB32 >  
Lesson AB33 >  
Vocabulary >  
 

I. Strings and Characters page 11 of 17

  1. It is natural to think of a char as a String of length 1. Unfortunately, in Java the char and String types are incompatible since a String is an object and a char is a primitive type. This means that you cannot use a String in place of a char or use a char in place of a String.

  2. Extracting a char from within a String can be accomplished using the charAt method as previously described.

  3. Conversion from char to String can be accomplished by using the "+" (concatenation) operator described previously. Concatenating any char with an empty string (String of length zero) results in a string that consists of that char. The java notation for an empty string is two consecutive double quotation marks. For example, to convert myChar to a String it is added to “”.

    char myChar = ‘X’;
    String myString = "" + myChar;
    System.out.println(myString);
    char anotherChar = ‘Y’;
    myString += anotherChar;
    System.out.println(myString);

    The output of this block of code would be:

    X
    XY

 

Main Previous Next
Contact
 © ICT 2006, All Rights Reserved.